/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



 


 	package edu.upf.grupE.interftest;



 	import java.awt.Dimension;




 	import java.awt.Graphics2D;




 	import java.awt.Image;




 	import java.awt.image.BufferedImage;



 	import java.awt.image.RenderedImage;



 	import java.awt.image.renderable.ParameterBlock;



 	import javax.media.jai.Interpolation;




 	import javax.media.jai.JAI;



 	import javax.media.jai.PlanarImage;


 	import javax.media.jai.RenderedOp;



 	import javax.swing.Icon;


 	import javax.swing.ImageIcon;



 	import javax.swing.JSlider;




 	public class Zoom extends javax.swing.JFrame {


 	    float xscale = 1.0F;




 	    float yscale = 1.0F;


 	        ImageSelection im;


 	    String path;



 	  Icon imageIcon;




 	    private ImageIcon scale(Image src, double scale) {




 	        int w = (int)(scale*src.getWidth(this));




 	        int h = (int)(scale*src.getHeight(this));



 	        int type = BufferedImage.TYPE_INT_RGB;


 	        BufferedImage dst = new BufferedImage(w, h, type);


 	        Graphics2D g2 = dst.createGraphics();




 	        g2.drawImage(src, 0, 0, w, h, this);



 	        g2.dispose();




 	        return new ImageIcon(dst);


 	    }



 	    /** Creates new form Contrast */


 	    public Zoom(ImageSelection im, String path) {
 	        this.im=im;
 	        this.path=path;
	        imageIcon = new ImageIcon(path);
 	        initComponents();
 	    }



 	    @SuppressWarnings("unchecked")

          // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents






 	    private void initComponents() {










 	        this.setResizable(false);


 	        jLabel1 = new javax.swing.JLabel();


 	        jSlider1 = new javax.swing.JSlider(0,2);




 	        jLabel2 = new javax.swing.JLabel();






 	        jTextField1 = new javax.swing.JTextField();



 	        jButton1 = new javax.swing.JButton();



 	        jButton2 = new javax.swing.JButton();


 	        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);




 	        setTitle("Zoom");



 	        jSlider1.setPaintLabels(true);


 	        jSlider1.setPaintTicks(true);
 
 	        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {


 	            public void stateChanged(javax.swing.event.ChangeEvent evt) {


 	                jSlider1StateChanged(evt);



 	            }

 	        });


 	        jLabel2.setText("Zoom");


 	        jButton1.setText("OK");


 	        jButton1.addActionListener(new java.awt.event.ActionListener() {



 	            public void actionPerformed(java.awt.event.ActionEvent evt) {



 	                jButton1ActionPerformed(evt);


 	            }



 	        });


 	        jButton2.setText("Cancel");


 	        jButton2.addActionListener(new java.awt.event.ActionListener() {


 	            public void actionPerformed(java.awt.event.ActionEvent evt) {


 	                jButton2ActionPerformed(evt);


 	            }




 	        });


 	        jTextField1.setEnabled(false);



 	        ImageIcon icon= new ImageIcon(path);

 	        ImageIcon reduced= scale(icon.getImage(), 0.40);


 	        jLabel1.setIcon(reduced);


 	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

 	        getContentPane().setLayout(layout);



 	        layout.setHorizontalGroup(


 	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)


 	            .addGroup(layout.createSequentialGroup()



 	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)



 	                    .addGroup(layout.createSequentialGroup()

 	                        .addGap(45, 45, 45)

 	                        .addComponent(jLabel1))



 	                    .addGroup(layout.createSequentialGroup()


 	                        .addGap(28, 28, 28)


 	                        .addComponent(jLabel2)



 	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)



 	                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)


 	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)


 	                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))


 	                    .addGroup(layout.createSequentialGroup()


 	                        .addGap(115, 115, 115)

 	                        .addComponent(jButton1)



 	                        .addGap(18, 18, 18)


 	                        .addComponent(jButton2)))



 	                .addContainerGap(513, Short.MAX_VALUE))

 	        );


 	        layout.setVerticalGroup(



 	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)



 	            .addGroup(layout.createSequentialGroup()


 	                .addGap(19, 19, 19)


 	                .addComponent(jLabel1)

 	                .addGap(18, 18, 18)



 	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)


 	                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)


 	                    .addComponent(jLabel2)


 	                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))


 	                .addGap(18, 18, 18)


 	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)



 	                    .addComponent(jButton2)




 	                    .addComponent(jButton1))


 	                .addContainerGap(456, Short.MAX_VALUE))



 	        );



 	        pack();


 	    }// </editor-fold>
 	public void repaint() {



 	        // Beware of a race condition when Swing tries to repaint



 	        // before we've finished initialization.



 	        if(imageIcon instanceof IconJAI) {


 	            RenderedImage previousImage  = ((IconJAI)imageIcon).getImage();



 	            if(previousImage instanceof PlanarImage) {



 	                ((PlanarImage)previousImage).dispose();



 	            }


 	        }



 	        imageIcon = new IconJAI(process());



 	        jLabel1.setIcon(imageIcon);



 	        Dimension iconSize = new Dimension(imageIcon.getIconWidth(),


 	                                           imageIcon.getIconHeight());


 	        jLabel1.setPreferredSize(iconSize);



 	        jLabel1.revalidate();



 	    }



 	     public PlanarImage process() {



 	        PlanarImage im = (PlanarImage)JAI.create("fileload", path);



 	        ParameterBlock pb = new ParameterBlock();


 	        Interpolation interp = Interpolation.getInstance(



 	                                        Interpolation.INTERP_BILINEAR);



 	        ParameterBlock params = new ParameterBlock();


 	        params.addSource(im);




 	        params.add(0.5F);         // x scale factor



 	        params.add(0.5F);         // y scale factor



 	        params.add(0.0F);         // x translate


 	        params.add(0.0F);         // y translate


 	        params.add(interp);       // interpolation method


 	        /* Create an operator to scale image1. */

 	        RenderedOp image2 = JAI.create("scale", params);



 	        pb.addSource(image2);



 	        pb.add(xscale);



 	        pb.add(yscale);


 	        pb.add(0.0F);



 	        pb.add(0.0F);



 	        // pb.add(xtrans);



 	        // pb.add(ytrans);




 	        if (interp == null) {


 	            interp = Interpolation.getInstance(Interpolation.INTERP_NEAREST);

 	        }



 	        pb.add(interp);



 	        return JAI.create("scale", pb);



 	    }



 	     private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider2StateChanged




 	        // TODO add your handling code here:



 	        jTextField1.setText(Integer.toString(jSlider1.getValue()));



 	        JSlider source = (JSlider)evt.getSource();



 	        float value = source.getValue();




 	        float fvalue = value/1.0F + 1.0F;


 	        if (value < 0) {



 	            fvalue = 1.0F/fvalue;


 	       }



 	        if (source == jSlider1) {



 	            xscale = yscale = fvalue;



 	        }



 	        repaint();




 	    }




 	   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed



 	        // TODO add your handling code here:


 	           this.dispose();


 	           im.setIcon(imageIcon);





 	    }



 	    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed


 	        // TODO add your handling code here:




 	           this.dispose();







 	    }//GEN-LAST:event_jButton2ActionPerformed


 	    /**




 	    * @param args the command line arguments




 	    */




 	    public static void main(String args[]) {




 	        java.awt.EventQueue.invokeLater(new Runnable() {



 	            public void run() {



 	//                new Zoom().setVisible(true);





 	            }




 	        });




 	    }



 	    // Variables declaration - do not modify//GEN-BEGIN:variables



 	    private javax.swing.JButton jButton1;


 	    private javax.swing.JButton jButton2;


 	    private javax.swing.JLabel jLabel1;



 	    private javax.swing.JLabel jLabel2;



 	    private javax.swing.JSlider jSlider1;



 	    private javax.swing.JTextField jTextField1;



 	    //private org.jdesktop.swingx.JXLabel jXLabel2;



 	    // End of variables declaration//GEN-END:variables



 	}